/* *****************************************************************************
 * JFab (http://code.google.com/p/jfab)
 * Copyright (c) 2011 JFab.org
 * Admin jfab@jeffreythompson.net
 *
 * See the file "LICENSE.txt" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * *****************************************************************************/

package org.jfab.core.factory;

import java.lang.annotation.Annotation;
import java.util.Collections;

import org.jfab.core.collection.CGAnnotationList;
import org.jfab.core.collection.ParameterNameList;
import org.jfab.core.model.CGAnnotation;
import org.jfab.core.model.CGType;

/**
 * Provides an annotation factory.
 */
public final class DefaultCGAnnotationFactory implements CGAnnotationFactory
{
    /** Type factory. */
    private final CGTypeFactory _typeFactory;

    /**
     * Construct this object with the given parameter.
     * 
     * @param typeFactory Type factory.
     */
    public DefaultCGAnnotationFactory(final CGTypeFactory typeFactory)
    {
        _typeFactory = typeFactory;
    }

    @Override
    public final CGAnnotation createAnnotation(final Annotation annotation)
    {
        final CGType cgType = getTypeFactory().createType(
                annotation.annotationType());

        return createAnnotation(cgType);
    }

    @Override
    public final CGAnnotation createAnnotation(final CGType type)
    {
        final String attribute = null;

        return createAnnotation(type, attribute);
    }

    @Override
    public final CGAnnotation createAnnotation(final CGType type,
            final ParameterNameList attributes)
    {
        final CGAnnotation answer = new CGAnnotation();

        answer.setType(type);

        if (attributes != null)
        {
            answer.getAttributes().addAll(attributes);
        }

        return answer;
    }

    @Override
    public final CGAnnotation createAnnotation(final CGType type,
            final String attribute)
    {
        ParameterNameList attributes = null;

        if (attribute != null)
        {
            attributes = new ParameterNameList(
                    Collections.singletonList(attribute));
        }

        return createAnnotation(type, attributes);
    }

    @Override
    public final CGAnnotationList createAnnotations(
            final Annotation[] annotations)
    {
        final CGAnnotationList answer = new CGAnnotationList();

        for (final Annotation annotation : annotations)
        {
            answer.add(createAnnotation(annotation));
        }

        return answer;
    }

    @Override
    public final CGAnnotation createOverrideAnnotation()
    {
        final CGType cgType = getTypeFactory().createType(Override.class);

        return createAnnotation(cgType);
    }

    /**
     * @return the typeFactory
     */
    private CGTypeFactory getTypeFactory()
    {
        return _typeFactory;
    }
}
